package bank.client;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import bank.Account;
import bank.Bank;
import bank.InactiveException;
import bank.OverdrawException;
import bank.transport.Answer;
import bank.transport.Request;

public class SocketBank implements Bank {

	private SocketServer sockserv;// = new SocketServer("localhost",6789);
	
	public SocketBank(SocketServer communicator) {
		this.sockserv = communicator;
	}

	public String createAccount(String owner) throws IOException {
		Answer a = sockserv.send(new Request("createAccount",owner));
		return a.str1;
	}

	public boolean removeAccount(String number) throws IOException {
		Answer a = sockserv.send(new Request("removeAccount",number));
		return a.bool1;
	}

	public Set<String> getAccountNumbers() throws IOException {
		Answer a = sockserv.send(new Request("getAccountNumbers"));
		Set<String> mySet = new HashSet<String>();
 
		for(int i = 0; i < a.sstr1.length;i++){
			if(a.sstr1[i] != null) {
				mySet.add(a.sstr1[i]);
			}
		}
		return mySet;
	}

	public Account getAccount(String number) throws IOException {
		Answer a = sockserv.send(new Request("getAccount",number));
		return a.acc1;
	}

	public void transfer(Account a, Account b, double amount)
			throws IOException, IllegalArgumentException, OverdrawException,
			InactiveException {
		sockserv.send(new Request("transfer",a,b,amount));	
	}
}

	